package cn.bone.base.component;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.*;
import java.util.Date;

@Component
public class FileSaveComponent {

    /*@Resource
    private GlobalParam globalParam;

    *//**
     * 保存文件到本地硬盘
     * @param is        输入流
     * @param suffix    文件后缀
     * @param type      类别
     * @return          文件访问地址
     *//*
    public String save(InputStream is, String suffix, String type){

        Date date = new Date();
        FastDateFormat format = FastDateFormat.getInstance("yyyy/MM/dd");
        String dateStr = format.format(date);
        String myType;
        if(StringUtil.isEmpty(type)){
            myType = "default";
        }else{
            myType = type;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(globalParam.getUploadDir());
        sb.append("/");
        sb.append(myType);
        sb.append("/");
        sb.append(dateStr);
        sb.append("/");
        sb.append(StringUtil.randomName());
        if(suffix.startsWith(".")){
            sb.append(suffix);
        }else{
            sb.append(".");
            sb.append(suffix);
        }
        String baseUrl = sb.toString();
        String fileUrl = globalParam.getBaseDir() + baseUrl;
        File file = new File(fileUrl);
        File parentFile = file.getParentFile();
        if(!parentFile.exists()){
            parentFile.mkdirs();
        }
        try (FileOutputStream fos = new FileOutputStream(file)){
            IOUtils.copy(is,fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    is = null;
                }
            }
        }
        return baseUrl;
    }
    *//**教师上传文件包到本地文件*//*
    public String teacherSave(InputStream is, String type,String name,String randomName){
        Date date = new Date();
        FastDateFormat format = FastDateFormat.getInstance("yyyy/MM/dd");
        String dateStr = format.format(date);
        String myType;
        if(StringUtil.isEmpty(type)){
            myType = "default";
        }else{
            myType = type;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(globalParam.getUploadDir());
        sb.append("/");
        sb.append(myType);
        sb.append("/");
        sb.append(dateStr);
        sb.append("/");
        sb.append(randomName);
        sb.append("/");
        sb.append(name);
        String baseUrl = sb.toString();
        String fileUrl = globalParam.getBaseDir() + baseUrl;
        File file = new File(fileUrl);
        File parentFile = file.getParentFile();
        if(!parentFile.exists()){
            parentFile.mkdirs();
        }
        try (FileOutputStream fos = new FileOutputStream(file)){
            IOUtils.copy(is,fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    is = null;
                }
            }
        }
        return baseUrl;
    }*/
}
